package com.chhuang.ewcrm;

import com.chhuang.ewcrm.application.LoginApp;
import com.chhuang.ewcrm.dao.WCameraDao;
import javafx.application.Application;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;

/**
 * 程序启动类
 * @author chhuang
 * @create 2020-09-29-9:30
 */
public class App {
    private static Logger logger = LoggerFactory.getLogger(App.class);

    private static SqlSession session;

    private App(){
        try {
            InputStream inputStream = Resources.getResourceAsStream("config/mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            session = sqlSessionFactory.openSession();
            session = sqlSessionFactory.openSession(true);//自动commit()提交
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSession(){
        if(session==null) new App();
        return session;
    }


    /**
     * 程序Socket服务端口
     */
    private static final int APP_PORT = 19912;

    public static void main(String[] args) {
        //防止程序重复打开
        try {
            ServerSocket socket = new ServerSocket(APP_PORT);
        } catch (IOException e) {
            e.printStackTrace();
            //提示程序已经打开
            JOptionPane.showMessageDialog(null, "过磅系统已经打开了", "错误信息",JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }

//        WCameraDao wCameraDao = App.getSession().getMapper(WCameraDao.class);
//        wCameraDao.query(null);

        Application.launch(LoginApp.class, args);
    }

}
